package com.dongnaoedu.threadlocaltest;

/**
 * 动脑学院-Mark老师
 * 创建日期：2017/11/28
 * 创建时间: 22:04
 */
public class ThreadLocalTest17 {

    static ThreadLocal<String> threadLocal = new ThreadLocal<String>() {
        @Override
        protected String initialValue() {
            return "init";
        }
    };


    private static class T1 implements Runnable {

        private int id;

        public T1(int id) {
            this.id = id;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getId() + " start");
            String s = threadLocal.get();
            s = s + "_" + id;
            threadLocal.set(s);
            System.out.println(Thread.currentThread().getId() + s);
        }
    }

    public static void main(String[] args) {
        ThreadLocalTest17 test = new ThreadLocalTest17();
        test.test();
    }

    public void test() {
        Thread[] runs = new Thread[3];
        for (int i = 0; i < runs.length; i++) {
            runs[i] = new Thread(new T1(i));
        }
        for (int i = 0; i < runs.length; i++) {
            runs[i].start();
        }
    }

}
